<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/4/22 16:33 jiahe_new OpensslEncrypt.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\common\common\lib;

class  OpensslEncrypt {

    const IV = "d89fb057f6d4f03g";//加密向量，16个字节
    const KEY = 'e9c8e878ee8e2658';//密钥，16个字节

    /**
     * 加密字符串
     * @param string $strContent 待加密的字符串内容
     * @param string $key 加密key
     * @return string 返回加密后的字符串，失败返回false
     */
    public static function encrypt($strContent,$key = self::KEY,$iv = self::IV){
        $strEncrypted = openssl_encrypt($strContent,"AES-128-CBC", $key,OPENSSL_RAW_DATA, $iv);
        return base64_encode($strEncrypted);
    }

    /**
     * 解密字符串
     * @param string $strEncryptCode加密后的字符串
     * @param string $key 加密key
     * @return string 返回解密后的字符串，失败返回false
     */
    public static function decrypt($strEncryptCode,$key = self::KEY,$iv = self::IV){
        $strEncrypted = base64_decode($strEncryptCode);
        return openssl_decrypt($strEncrypted,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv);
    }
}  